package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityXfSignOrder;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.CommonUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoXfSignOrder;
import com.ocom.payhandle.service.XfSignOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class XfSignOrderServiceImpl extends ServiceImpl<IDaoXfSignOrder, EntityXfSignOrder> implements XfSignOrderService {


    @Autowired
    IDaoXfSignOrder iDaoXfSignOrder;

    private  static  String key = "6iQq6ZyiCQ4ue3WFbeCTTlFYLpzLwSMG";

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public String getSignPhone(Long companyId,Long merchId ,Long pId) {
        QueryWrapper<EntityXfSignOrder> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("merch_id",merchId);
        queryWrapper.eq("p_id",pId);
        queryWrapper.eq("sign_type",1);
        queryWrapper.eq("status",2);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 1");
        EntityXfSignOrder entityXfSignOrder =  this.getOne(queryWrapper);
        if(entityXfSignOrder!=null && entityXfSignOrder.getBank()!=null){
            String phone = decode(companyId,entityXfSignOrder.getBank(),key) ;
            if(!CommonUtil.isNull(phone) && phone.length()==11){
                return phone;
            }
        }
        return null;
    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public String decode(Long companyId, String phone, String key) {
        return iDaoXfSignOrder.decode(phone,key);
    }

}
